package com.meiqiang.agent.redefine;

import com.meiqiang.po.Fruit;

import java.lang.instrument.ClassDefinition;
import java.lang.instrument.Instrumentation;
import java.lang.instrument.UnmodifiableClassException;

/**
 * 我们可以直观地从方法的名字上来理解它的作用，重定义class，通俗点来讲的话就是实现指定类的替换
 * @author : zhengmeiqiang
 * @date : 2024/11/8 14:00
 */
public class RedefineAgent {

    public static void premain(String agentArgs, Instrumentation inst)
            throws UnmodifiableClassException, ClassNotFoundException {
        String fileName = "D:\\idea_pj\\java-agent\\Fruit.class";
        ClassDefinition def = new ClassDefinition(Fruit.class,
                FruitTransformer.getClassBytes(fileName));
        inst.redefineClasses(new ClassDefinition[]{def});
    }
}
